<?php

class Admin_EventController extends Zend_Controller_Action
{
	
	public function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			if($this->_request->getActionName() != "login"){
				$this->_redirect("/admin/index/login");
			}
		}
	}

    public function init()
    {
        /* Initialize action controller here */
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->_model = new Default_Model_DbTable_Event();
    	$this->view->fullName = $infoUser->USERNAME;
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    }

    public function indexAction()
    {
        // action body
        $this->view->headTitle("Sự Kiện","PREPEND");
        $this->view->title = "Sự Kiện";
        $event = $this->_model->GetAll();
        $paginator = Zend_Paginator::factory($event);
        $paginator->setItemCountPerPage(9);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam("page",1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->allEvent = $paginator;
    }
    
    public function addAction(){
    	$this->view->headTitle("Thêm Mới Sự Kiện","PREPEND");
    	$this->view->title = "Thêm Mới Sự Kiện";
    	$upload = new Private_Upload();
    	
    	if($this->getRequest()->isPost()){
    		if($this->_getParam("EName")==""){
    			return $this->view->errN = "Chưa Nhập Tên Event";
    		}else{
    			$eName = $this->_request->getParam("EName");
    		}
    		if($_FILES['event']['name']==null && $this->_getParam("Link") == ""){
    			$img = "/ishali/public/uploaded/images/no_img.jpg";
    		}elseif (isset($_FILES["event"]["name"])){
    			$img = "/ishali/public/uploaded/images/event/".$_FILES['event']["name"];
    			$upload->upload("event");
    		}else{
    			$img = $this->_getParam("Link");
    		}
    		if($this->_getParam("DateOn")==""){
    			return $this->view->errDon = "Chưa Nhập Ngày Bắt Đầu";
    		}else{
    			$don = date("Y-m-d",strtotime($this->_request->getParam("DateOn")));
    		}
    		
    		if($this->_getParam("DateOff")==""){
    			return $this->view->errDoff = "Chưa Nhập Ngày Kết Thúc";
    		}else{
    			$doff = date("Y-m-d",strtotime($this->_request->getParam("DateOff")));
    		}
    		if($this->_getParam("SDesc")==""){
    			return $this->view->errSD = "Chưa Nhập Giới Thiệu";
    		}else{
    			$sDesc = $this->_request->getParam("SDesc");
    		}
    		if($this->_getParam("LDesc")==""){
    			return $this->view->errLD = "Chưa Nhập Mô Tả";
    		}else{
    			$lDesc = $this->_request->getParam("LDesc");
    		}
    		$stt = $this->_request->getParam("stt");
    		$this->_model->AddEvent($eName, $img, $don, $doff, $sDesc, $lDesc, $stt);
    		$this->_redirect("/admin/event");
    	}
    }
    public function editAction(){
    	$this->view->headTitle("Chỉnh Sửa Sự Kiện","PREPEND");
    	$this->view->title = "Chỉnh Sửa Sự Kiện";
    	$upload = new Private_Upload();
    	
    	$eId = $this->_getParam("id",0);
    	$event = $this->_model->GetById($eId);
    	$this->view->Event = $event;
    	
    	if($this->getRequest()->isPost()){
    	if($this->_getParam("EName")==""){
    			return $this->view->errN = "Chưa Nhập Tên Event";
    		}else{
    			$eName = $this->_request->getParam("EName");
    		}
    		if($_FILES['event']['name']==null && $this->_getParam("Link") == ""){
    			$img = "/ishali/public/uploaded/images/no_img.jpg";
    		}elseif ($_FILES["event"]["name"]){
    			$img = "/ishali/public/uploaded/images/event/".$_FILES['event']["name"];
    			$upload->upload("event");
    		}else{
    			$img = $this->_getParam("Link");
    		}
    		if($this->_getParam("DateOn")==""){
    			return $this->view->errDon = "Chưa Nhập Ngày Bắt Đầu";
    		}else{
    			$don = date("Y-m-d",strtotime($this->_request->getParam("DateOn")));
    		}
    		
    		if($this->_getParam("DateOff")==""){
    			return $this->view->errDoff = "Chưa Nhập Ngày Kết Thúc";
    		}else{
    			$doff = date("Y-m-d",strtotime($this->_request->getParam("DateOff")));
    		}
    		if($this->_getParam("SDesc")==""){
    			return $this->view->errSD = "Chưa Nhập Giới Thiệu";
    		}else{
    			$sDesc = $this->_request->getParam("SDesc");
    		}
    		if($this->_getParam("LDesc")==""){
    			return $this->view->errLD = "Chưa Nhập Mô Tả";
    		}else{
    			$lDesc = $this->_request->getParam("LDesc");
    		}
    		$stt = $this->_getParam("stt");
    			$this->_model->UpdateEvent($eId,$eName, $img, $don, $doff, $sDesc, $lDesc, $stt);
    			$this->_redirect("/admin/event");
    	}
    }
    
    public function delAction(){
    	$this->_model->DelEvent($this->_getParam("id"));
    	$this->_redirect("/admin/event");
    }
    
    public function activeAction(){
    	$id = $this->_getParam("id");
    	$this->_model->changeStatus($id);
    	$this->_redirect("/admin/event");
    }


}

